//
//  ChooseCarLayer.m
//  CarDemo
//
//  Created by huang kun on 8/9/10.
//  Copyright 2010 UESTC. All rights reserved.
//

#import "ChooseCarLayer.h"
#import "ChooseMapLayer.h"
#import "MenuLayer.h"


#define ktagScrollView 1
#define ktagLeftButton 2
#define ktagRightButton 3

@implementation ChooseCarLayer
@synthesize carIndex;

- (void) dealloc {
	[self removeAllChildrenWithCleanup:YES];
	[super dealloc];
}


- (id) init {
	if( (self = [super init]) ){

		//CGSize winSize = [[CCDirector sharedDirector] winSize];
		
		/*	isTouchEnabled = YES;
		CGSize winSize = [[CCDirector sharedDirector] winSize];

		
		CCSprite *bg = [CCSprite spriteWithFile:@"chooseCarBg.png"];
		[self addChild:bg];
		[bg setPosition:ccp(winSize.width/2, winSize.height/2)];
		
		
		CCMenuItemImage *menuButton = [CCMenuItemImage itemFromNormalImage:@"" selectedImage:@"" 
																	 target:self selector:@selector(backToMenu:)];
		CCMenuItemImage *selectButton = [CCMenuItemImage itemFromNormalImage:@"" selectedImage:@""
																		target:self selector:@selector(continueGame:)];
																	target:self selector:@selector(didSelected:)];
		
		CCMenu *menu = [CCMenu menuWithItems:menuButton,selectButton,nil];
		[self addChild:menu];
		
		[menuButton setPosition:ccp(103, 15)];
		[selectButton setPosition:ccp(379, 15)];
	 */
		
		
		UIScrollView *view = [[[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)] autorelease];
		view.contentSize = CGSizeMake(320, 480*4);
		[view setDelegate:self];
		[view setTag:ktagScrollView];
		
		for (int i=0; i< 4; i++) {
			UIImageView *imageView = [[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)] autorelease];
			[imageView setImage:[UIImage imageNamed:[NSString stringWithFormat:@"chooseCar%d.png",i+1]]];
			[view addSubview:imageView];
			imageView.center = CGPointMake(160, 240+i*480);
			imageView.tag = i+1;
		}	
		view.scrollEnabled = YES;
		view.bounces = YES;
		view.showsVerticalScrollIndicator = NO;
		view.showsHorizontalScrollIndicator = NO;
		view.clipsToBounds = YES;
		view.pagingEnabled = YES;
		
		UIButton *back = [[[UIButton alloc] initWithFrame:CGRectMake(0,0,31,87)] autorelease];
		[back addTarget:self action:@selector(backToMenu:) forControlEvents:UIControlEventTouchUpInside];
		[back setImage:[UIImage imageNamed:@"menu01.png"] forState:UIControlStateNormal];
		[back setImage:[UIImage imageNamed:@"menu03.png"] forState:UIControlStateSelected];
		[back setCenter:ccp(15, 101)];	
		
		UIButton *select = [[[UIButton alloc] initWithFrame:CGRectMake(0,0,31,88)] autorelease];
		[select addTarget:self action:@selector(didSelected:) forControlEvents:UIControlEventTouchUpInside];
		[select setImage:[UIImage imageNamed:@"select01.png"] forState:UIControlStateNormal];
		[select setImage:[UIImage imageNamed:@"select03.png"] forState:UIControlStateSelected];
		[select setCenter:ccp(15, 377)];
		
		UIImageView *imageView1 = [[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 66, 425)] autorelease];
		[imageView1 setImage:[UIImage imageNamed:@"chooseCarMenu.png"]];
		[imageView1 setCenter:ccp(32, 240)];
		
		
		leftSide = [[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 36)] autorelease];
		[leftSide setImage:[UIImage imageNamed:@"leftSide1.png"]];
		[leftSide setCenter:ccp(160, 17)];
		
		rightSide = [[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 33)] autorelease];
		[rightSide setImage:[UIImage imageNamed:@"rightSide1.png"]];
		[rightSide setCenter:ccp(160, 464)];
		
		
		/*UIImageView *leftButton = [[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 71, 67)] autorelease];
		[leftButton setImage:[UIImage imageNamed:@"chooseButtonLeft.png"]];
		[leftButton setCenter:ccp(174, 46)];
		
		UIImageView *rightButton = [[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 71, 67)] autorelease];
		[rightButton setImage:[UIImage imageNamed:@"chooseButtonRight.png"]];
		[rightButton setCenter:ccp(174, 435)];
		*/
		
		UIButton *leftButton = [[[UIButton alloc] initWithFrame:CGRectMake(0, 0, 71, 67)] autorelease];
		[leftButton addTarget:self action:@selector(didChoose:) forControlEvents:UIControlEventTouchUpInside];
		[leftButton setImage:[UIImage imageNamed:@"chooseButtonLeft.png"] forState:UIControlStateNormal];
		[leftButton setImage:[UIImage imageNamed:@"chooseButtonLeftPressed.png"] forState:UIControlStateSelected];
		[leftButton setCenter:ccp(174, 46)];
		[leftButton setTag:ktagLeftButton];
		
		
		UIButton *rightButton = [[[UIButton alloc] initWithFrame:CGRectMake(0, 0, 71, 67)] autorelease];
		[rightButton addTarget:self action:@selector(didChoose:) forControlEvents:UIControlEventTouchUpInside];
		[rightButton setImage:[UIImage imageNamed:@"chooseButtonRight.png"] forState:UIControlStateNormal];
		[rightButton setImage:[UIImage imageNamed:@"chooseButtonRightPressed.png"] forState:UIControlStateSelected];
		[rightButton setCenter:ccp(174, 435)];	
		[rightButton setTag:ktagRightButton];
		
		
		light = [[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 169, 184)] autorelease];
		[light setImage:[UIImage imageNamed:@"light.png"]];
		[light setCenter:ccp(190, 231)];
		
		
		
		UIView *openGLView = [[CCDirector sharedDirector] openGLView] ;
		[openGLView addSubview:view];
		[openGLView addSubview:imageView1];
		[openGLView addSubview:back];
		[openGLView addSubview:select];
		[openGLView addSubview:leftSide];
		[openGLView addSubview:rightSide];
		[openGLView addSubview:leftButton];
		[openGLView addSubview:rightButton];
		[openGLView addSubview:light];
	}
	return self;
}

- (void) didChoose:(id) sender {
	UIButton *button = (UIButton *)sender;
	UIView *openGLView = [[CCDirector sharedDirector] openGLView] ;
	UIScrollView *view = (UIScrollView *)[openGLView viewWithTag:ktagScrollView];
	switch (button.tag) {
		case ktagLeftButton:
		{
			if (!view.dragging && !view.decelerating && !view.tracking && view.contentOffset.y >= 480){
				[view setContentOffset:ccp(view.contentOffset.x, view.contentOffset.y-480) animated:YES];
				light.alpha = 0;
			}
		}
			break;
		case ktagRightButton:
		{
			if (!view.dragging && !view.decelerating && !view.tracking && view.contentOffset.y <= 960){
				[view setContentOffset:ccp(view.contentOffset.x, view.contentOffset.y+480) animated:YES];
				light.alpha = 0;
			}
		}
			break;
		default:
			break;
	}
}
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
	light.alpha = 0;
}


- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
	[self setCarIndex:(int)scrollView.contentOffset.y / 480 + 1];
	light.alpha = 1;
}
- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView {
	[self setCarIndex:(int)scrollView.contentOffset.y / 480 + 1];
	light.alpha = 1;
}

- (void) setCarIndex:(int) index {
	if (index >= 1 && index <= 4){
		carIndex = index;
		[leftSide setImage:[UIImage imageNamed:[NSString stringWithFormat:@"leftSide%d.png",carIndex]]];
		[rightSide setImage:[UIImage imageNamed:[NSString stringWithFormat:@"rightSide%d.png",carIndex]]];
	}
}

- (void) backToMenu:(id)sender {
	UIView *openGLView = [[CCDirector sharedDirector] openGLView] ;
	for (UIView *view in [openGLView subviews]) 
		[view removeFromSuperview];
	
	[[CCTextureCache sharedTextureCache] removeUnusedTextures];
	[[CCTextureCache sharedTextureCache] removeAllTextures];
	[[CCDirector sharedDirector] replaceScene:[[CCScene node] addChild:[MenuLayer node]]];
}

- (void) didSelected:(id)sender {
	UIView *openGLView = [[CCDirector sharedDirector] openGLView] ;
	for (UIView *view in [openGLView subviews]) 
		[view removeFromSuperview];
	
	[[CCTextureCache sharedTextureCache] removeUnusedTextures];
	[[CCTextureCache sharedTextureCache] removeAllTextures];
	[[CCDirector sharedDirector] replaceScene:[[CCScene node] addChild:[ChooseMapLayer node]]];
}

@end
